<template>
  <div class="mol-user">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="账号">
        <span>{{ userName }}</span>
      </el-form-item>
      <el-form-item label="原密码" prop="password">
        <el-input
          type="password"
          v-model="dataForm.password"
          style="width: 40%"
        ></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          type="password"
          v-model="dataForm.newPassword"
          @input="handleDisabled()"
          style="width: 40%"
        ></el-input>
        <el-tag type="danger" class="tag">密码请尽量包含数字，字母，特殊符号</el-tag>
        <el-progress
          style="width: 30%"
          v-if="disabled"
          :percentage="percentage"
          :color="customColors"
          :format="format"
          :text-inside="true"
          :stroke-width="24"
        ></el-progress>
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          type="password"
          style="width: 40%"
          v-model="dataForm.confirmPassword"
        ></el-input>
        <el-tag type="danger" class="tag">密码请尽量包含数字，字母，特殊符号</el-tag>
      </el-form-item>
    </el-form>
    <span slot="footer" class="footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </div>
</template>

<script>
import { clearLoginInfo } from "@/utils";
import {
  isNum,
  isLetter,
  isNumAndLetter,
  isNumAndLetterAndSpecial,
} from "@/utils/validate";
export default {
  data() {
    var validateConfirmPassword = (rule, value, callback) => {
      if (this.dataForm.newPassword !== value) {
        callback(new Error("确认密码与新密码不一致"));
      } else {
        callback();
      }
    };
    return {
      visible: false,
      dataForm: {
        password: "",
        newPassword: "",
        confirmPassword: "",
      },
      dataRule: {
        password: [
          { required: true, message: "原密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { validator: validateConfirmPassword, trigger: "blur" },
        ],
      },
      percentage: 0,
      disabled: false,
      customColors: '#409eff'
    };
  },
  computed: {
    userName: {
      get() {
        return this.$store.state.user.name;
      },
    },
    mainTabs: {
      get() {
        return this.$store.state.common.mainTabs;
      },
      set(val) {
        this.$store.commit("common/updateMainTabs", val);
      },
    },
  },
  methods: {
    // 初始化
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
    },
    // 表单提交
    dataFormSubmit() {
      if(this.percentage<60){
        this.$message({
          message: '密码强度太弱，请重新输入密码',
          type: 'warning'
        });
        return
      }
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl("/sys/user/password"),
            method: "post",
            data: this.$http.adornData({
              password: this.dataForm.password,
              newPassword: this.dataForm.newPassword,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$nextTick(() => {
                    this.mainTabs = [];
                    clearLoginInfo();
                    this.$router.replace({ name: "login" });
                  });
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    handleDisabled() {
      if (this.dataForm.newPassword == "") {
        this.disabled = false;
      } else {
        this.disabled = true;
        if (
          (isNum(this.dataForm.newPassword) ||
            isLetter(this.dataForm.newPassword)) &&
          !isNumAndLetter(this.dataForm.newPassword)
        ) {
          this.customColors = '#e6a23c'
          this.percentage = 10;
        } else if (isNumAndLetter(this.dataForm.newPassword) && !isNumAndLetterAndSpecial(this.dataForm.newPassword)) {
          this.percentage = 66.66;
          this.customColors = '#5cb87a'
        } else if (isNumAndLetterAndSpecial(this.dataForm.newPassword)) {
          this.customColors = '#00FA9A';
          this.percentage = 100;
        }
      }
    },
    format(percentage) {
        console.log(percentage);
        if(percentage < 60){
          return "弱";
        }else if(percentage >= 60 && percentage <100){
          return "中";
        }else {
          return "强";
        }
        // return percentage === 100 ? '满' : `${percentage}%`;
    }

  },
};
</script>

<style>
.footer {
  margin-left: 30%;
}
.mol-user {
  /* position: relative; */
}
.tag {
  margin-left: 3%;
}
</style>
